package com.eb.utils;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;

/**
 * 数据格式化工具类
 * @author Administrator
 *
 */
public class NumberUtil {
	
	/**
	 * 小数-->百分比 保留 N位小数 
	 * 默认:四舍五入
	 * 2016-7-1
	 * v-haowang
	 */
	public static String percentFormat(double number,int n){
		NumberFormat nf = NumberFormat.getPercentInstance(); 
		nf.setMinimumFractionDigits(n);// 小数点后保留几位
		String str = nf.format(number);
		return str;
	}
	
	/**
	 * double 保留n位小数  四舍五入
	 * @return
	 */
	public static double formatDouble(double d,int n){
		String format="#.";
		for(int i=0;i<n;i++){
			format+="#";
		}
		return Double.valueOf(new DecimalFormat(format).format(d));
	}
	
	/**
	 * 数据格式化 
	 * #,###.00
	 * 2016-7-1
	 * v-haowang
	 */
	public static String numFormat(Object o){
		String str = "--";
		try{
			Format fm = new DecimalFormat("#,##0.00");
			str = fm.format(o);
		} catch (Exception e) {
			str = "--";
		}
		return str;
	}
	
	/**
	 * double转String(去掉科学计数法E)
	 * 1234E5.123--->
	 * 2016-7-1
	 * v-haowang
	 */
	public static String dToString(double d){
		String str = "--";
		try{
			Format fm = new DecimalFormat("#");
			str = fm.format(d);
		} catch (Exception e) {
			str = "--";
		}
		return str;
	}
	/**
	 * 获取6位随机数
	 * @return
	 */
	public static int getRandomNum(){
		int[] ret = new int[6];
		for (int i = 0; i < 6; i++) {
			int random=(int)(Math.random()*10);
			ret[i] = random;
		}
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < ret.length; i++) {
			sb.append(ret[i]);
		}
		return Integer.parseInt(sb.toString());
	}
	
	public static void main(String[] args) {
		Double d = 123456678.12*100;
		System.out.println(dToString(d));
	}
	
}
